package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemLongClickListener;
import com.king.image.imageviewer.ImageViewer;
import com.king.image.imageviewer.loader.GlideImageLoader;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.MyRecordAdapter;
import flc.ast.adapter.RecordAdapter;
import flc.ast.bean.MyRecordBean;
import flc.ast.databinding.ActivityRecordBinding;
import flc.ast.utils.AppSpUtils;

public class RecordActivity extends BaseAc<ActivityRecordBinding> {

    private MyRecordAdapter recordAdapter;
    private List<MyRecordBean> listShow=new ArrayList<>();
    private List<MyRecordBean> listSel=new ArrayList<>();
    private List<MyRecordBean> listAll=new ArrayList<>();
    private boolean isDelete=false;

    @Override
    protected int onCreate() {
        return R.layout.activity_record;
    }

    @Override
    protected void initView() {
        mDataBinding.ivRecordBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivMyRecordDelete.setOnClickListener(this);
        mDataBinding.ivMyRecordManage.setOnClickListener(this);
        mDataBinding.rvRecordList.setLayoutManager(new GridLayoutManager(mContext,2));
        recordAdapter=new MyRecordAdapter();
        recordAdapter.setDelete(isDelete);
        mDataBinding.rvRecordList.setAdapter(recordAdapter);
        recordAdapter.setOnItemClickListener(this);
        recordAdapter.setOnItemChildClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivMyRecordManage:
                isDelete=true;
                recordAdapter.setDelete(isDelete);
                recordAdapter.notifyDataSetChanged();
                mDataBinding.ivMyRecordManage.setVisibility(View.GONE);
                mDataBinding.ivMyRecordDelete.setVisibility(View.VISIBLE);
                break;
            case R.id.ivMyRecordDelete:
                isDelete=false;
                recordAdapter.setDelete(isDelete);
                recordAdapter.notifyDataSetChanged();
                mDataBinding.ivMyRecordManage.setVisibility(View.VISIBLE);
                mDataBinding.ivMyRecordDelete.setVisibility(View.GONE);
                listAll.removeAll(listSel);
                AppSpUtils.saveRecordList(listAll);
                getData();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (isDelete){
            if (recordAdapter.getItem(position).isSelected()){
                recordAdapter.getItem(position).setSelected(false);
                recordAdapter.notifyItemChanged(position);
                listSel.remove(recordAdapter.getItem(position));
            }else {
                listSel.add(recordAdapter.getItem(position));
                recordAdapter.getItem(position).setSelected(true);
                recordAdapter.notifyItemChanged(position);
            }
        }else {
            ImageViewer.load(recordAdapter.getItem(position).getPath())//要加载的图片数据，单张或多张
                    .selection(0)//当前选中位置，默认：0
                    .indicator(false)//是否显示指示器，默认不显示
                    .imageLoader(new GlideImageLoader())//加载器，imageLoader必须配置，目前内置的有GlideImageLoader或PicassoImageLoader，也可以自己实现
                    .theme(R.style.ImageViewerTheme)//设置主题风格，默认：R.style.ImageViewerTheme
                    .orientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)//设置屏幕方向,默认：ActivityInfo.SCREEN_ORIENTATION_BEHIND
                    .start(RecordActivity.this);//activity or fragment, 跳转时的共享元素视图
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData(){
        listShow.clear();
        listAll.clear();
        listSel.clear();
        List<MyRecordBean> listSp = AppSpUtils.getRecordList();
        if (listSp != null && listSp.size() > 0) {
            listShow.addAll(listSp);
            listAll.addAll(listSp);
            recordAdapter.setList(listShow);
            mDataBinding.rvRecordList.setVisibility(View.VISIBLE);
            mDataBinding.llRecordNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvRecordList.setVisibility(View.GONE);
            mDataBinding.llRecordNoData.setVisibility(View.VISIBLE);
        }
    }

}